package com.sens.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sens.alipay.utils.OrderUtil;
import com.sens.orderback.R;
import com.sens.tables.AlipayOrder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AlipayOrderAdapter extends BaseAdapter {
    private List<AlipayOrder> list = new ArrayList<>(10);
    private Map<AlipayOrder, CharSequence> marks = new HashMap<>(10);

    public void addOrder(AlipayOrder... alipayOrders) {
        Collections.addAll(list, alipayOrders);
    }

    public void addOrder(Collection<AlipayOrder> alipayOrders) {
        list.addAll(alipayOrders);
    }

    public void clear() {
        list.clear();
        marks.clear();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public AlipayOrder getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_alipay_order, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.updateOrder(getItem(position));
        return convertView;
    }

    class ViewHolder {
        private TextView tradeNoTV;
        private TextView moneyTV;
        private TextView markTV;
        private TextView timeTV;
        private TextView hasUpload;

        ViewHolder(View view) {
            tradeNoTV = view.findViewById(R.id.tradeNoTV);
            moneyTV = view.findViewById(R.id.moneyTV);
            markTV = view.findViewById(R.id.markTV);
            timeTV = view.findViewById(R.id.timeTV);
            hasUpload = view.findViewById(R.id.hasUpload);
        }

        void updateOrder(AlipayOrder alipayOrder) {
            tradeNoTV.setText("单号：" + alipayOrder.getTradeNo());
            moneyTV.setText(alipayOrder.getMoney());
            CharSequence charSequence = marks.get(alipayOrder);
            if (charSequence == null) {
                charSequence = OrderUtil.getShowMark(alipayOrder);
                marks.put(alipayOrder, charSequence);
            }
            markTV.setText(charSequence);
            timeTV.setText(alipayOrder.getTime());
            if (alipayOrder.isHasUpload()) {
                hasUpload.setTextColor(0xFF0099CC);
                hasUpload.setText("已上传");
            } else {
                hasUpload.setTextColor(0xFFCC0000);
                hasUpload.setText("未上传");
            }
        }
    }
}
